/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.controller

import android.content.Context
import android.net.wifi.SoftApConfiguration
import android.os.Build
import com.ecarx.systemui.plugin.utils.LogUtils
//import ecarx.openapi.base.FunctionStatus
import ecarx.openapi.net.wifi.ApConfigurationListener
import ecarx.openapi.net.wifi.ApStateListener
import ecarx.openapi.net.wifi.ExternalWifiApManager
import ecarx.openapi.net.wifi.IExternalWifiAp

/**
 *  TCAM 热点
 */
class TcamHotspotController {

    companion object {
        private const val TAG = "TcamHotspotController"
    }

    var mWifiAp: IExternalWifiAp? = null

    fun init(context: Context) {
        ExternalWifiApManager.get(context).init { ready: Boolean ->
            if (ready) {
                mWifiAp = ExternalWifiApManager.get(context)
            }
        }
    }

    fun isAvailable(): Boolean{
//        return mWifiAp?.functionStatus == FunctionStatus.AVAILABLE
        return false;
    }

    fun registerHotspotState(tcamHotspotState: TcamHotspotState) {
        mWifiAp?.registerApStateListener(object : ApStateListener {
            override fun onStateChanged(state: Int) {
                if (state == IExternalWifiAp.WIFI_AP_STATE_ENABLED) {
                    tcamHotspotState.onStateChanged(true)
                } else if (state == IExternalWifiAp.WIFI_AP_STATE_DISABLED or IExternalWifiAp.WIFI_AP_STATE_FAILED) {
                    tcamHotspotState.onStateChanged(false)
                }
            }

        })
        mWifiAp?.registerApConfigurationListener(object : ApConfigurationListener {
            override fun onConfigurationChanged(config: SoftApConfiguration) {
                tcamHotspotState.onConfigurationChanged(config)
            }

        })
    }

    /**
     * IExternalWifiAp.WIFI_AP_STATE_DISABLING WIFI_AP_STATE_DISABLED WIFI_AP_STATE_ENABLING WIFI_AP_STATE_ENABLED WIFI_AP_STATE_FAILED
     */
    private fun getWifiApSate(): Int? {
        return mWifiAp?.wifiApState
    }

    fun isEnabled(): Boolean {
        return isAvailable() && getWifiApSate() == IExternalWifiAp.WIFI_AP_STATE_ENABLED
    }

    fun startHotspot() {
        LogUtils.d(TAG, "startHotspot() called with isEnabled=${isEnabled()}")
        mWifiAp?.startTetheredHotspot(mWifiAp?.softApConfiguration)
    }

    fun stopHotspot() {
        LogUtils.d(TAG, "stopHotspot() called with isEnabled=${isEnabled()}")
        mWifiAp?.stopSoftAp()
    }

    fun getNameAndPwd(): Pair<String?, String?> {
        val softApConfiguration = mWifiAp?.softApConfiguration
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            Pair<String?, String?>(softApConfiguration?.ssid, softApConfiguration?.passphrase)
        } else {
            Pair<String?, String?>("", "")
        }
    }

    fun setHotSpotName(name: String) {
        try {
            val softApConfiguration = mWifiAp?.softApConfiguration
            val config: SoftApConfiguration = SoftApConfiguration.Builder(softApConfiguration)
                .setSsid(name)
                .build()
            mWifiAp?.softApConfiguration = config
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun setHotSpotPwd(pwd: String) {
        try {
            val softApConfiguration = mWifiAp?.softApConfiguration
            val config: SoftApConfiguration = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                SoftApConfiguration.Builder(softApConfiguration)
                    .setPassphrase(pwd, softApConfiguration?.securityType ?: 0)
                    .build()
            } else {
                SoftApConfiguration.Builder(softApConfiguration)
                    .setPassphrase(pwd)
                    .build()
            }
            mWifiAp?.softApConfiguration = config
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    interface TcamHotspotState {
        fun onStateChanged(enable: Boolean)
        fun onConfigurationChanged(config: SoftApConfiguration)
    }
}